package com.tr.rentcar.common;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet extends HttpServlet {

	/**
	 * 统一处理异常
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			this.request = request;
			this.response = response;
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("UTF-8");
			super.service(request, response);
			System.out.println("回来了");
		} catch (Exception e) {
			e.printStackTrace();
			/*response.setStatus(500);
			this.forward("error");*/
		}
	}

	/**
	 * 转发
	 * @param page
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void forward(String page) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/jsp/" + page + ".jsp").forward(request, response);
	}

	/**
	 * 重定向
	 * @param path
	 * @throws IOException
	 */
	protected void redirect(String path) throws IOException {
		response.sendRedirect(request.getContextPath() + "/" + path);
	}

}
